package com.speily.example;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * 代码生成
 *
 * @author : SPL
 * @since : 2020-04-20 18:16
 **/
@SpringBootTest(classes = ExampleApplication.class)
public class GenerateCode {


    @Value("${spring.datasource.driverClassName}")
    private String driver;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

    private static String tempBasePath = "/temp/plus";

    //输出目录
    private static String outputDir = "D:\\MybatisGenerate";

    //基本包名
    private static String basePackage = "com.speily.example";

    //要生成的表名,和对应前缀
    private static String[] tables = {"biz_test"};
    private static String[] tables_prefix = {""};

    @Test
    void run() {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //1、 数据源配置
        DataSourceConfig db = new DataSourceConfig();
        db.setDriverName(driver);
        db.setUsername(username);
        db.setPassword(password);
        db.setUrl(url);
        mpg.setDataSource(db);

        //2、 全局配置
        GlobalConfig gc = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
//        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setOutputDir(outputDir);
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor("SPL");
//        gc.setDateType(DateType.ONLY_DATE);//Date类型

        //自定义文件命名
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        //3、 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(basePackage);
        pc.setModuleName(null);  //所属模块
//        pc.setController("");
//        pc.setEntity("");
//        pc.setService("");
//        pc.setServiceImpl("");
//        pc.setMapper("");
//        pc.setXml("");
        mpg.setPackageInfo(pc);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        templateConfig.setController(tempBasePath + "/controller.java");
        templateConfig.setEntity(tempBasePath + "/entity.java");
        templateConfig.setService(tempBasePath + "/service.java");
        templateConfig.setServiceImpl(tempBasePath + "/serviceImpl.java");
        templateConfig.setMapper(tempBasePath + "/mapper.java");
        templateConfig.setXml(tempBasePath + "/mapper.xml");

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        //4、生成策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude(tables); // 需要生成的表
        strategy.setTablePrefix(tables_prefix);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setLogicDeleteFieldName("del_flag");//逻辑删标识
        strategy.setRestControllerStyle(true);//RestController
        mpg.setStrategy(strategy);
        mpg.execute();
    }

}
